package com.smart.controller;

import java.util.Calendar;
import java.util.Collections;
import java.util.Map;

import org.json.JSONException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.qiniu.api.auth.AuthException;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.config.Config;
import com.qiniu.api.rs.PutPolicy;

@RestController
public class QiNiuController {
	@RequestMapping(value = "/qiniucloud/uploadtoken", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, String> getQiNiuToken() {
		// 设置好账号的ACCESS_KEY和SECRET_KEY
		Config.ACCESS_KEY = "tmuGYVOgfRRhZAw_AxCUmH97pHfLLz2TqvS5QVlm";
		Config.SECRET_KEY = "wnm28fbJaTdQY6ZTqgD8YFzmiRQnlncwCOgACtwK";
		Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
		
		// 要上传的空间
		String bucketName = "media-repository";
		PutPolicy putPolicy = new PutPolicy(bucketName);
		String uptoken = null;
		try {
			uptoken = putPolicy.token(mac);
		} catch (JSONException | AuthException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return Collections.singletonMap("uploadToken", uptoken);
	}
}
